<?php

/**
 * 后台管理 - 入口
 *
 * @author suning <snsnsky@gmail.com>
 */

use lib\core\AdminAction;
use lib\util\Securimage;
use Encrypt\Rsa;

define("NO_CHECK", true);

require('common.inc.php');

class index extends AdminAction
{
    /**
     * 程序入口
     */
    public function doDefault()
    {
        $smarty = $this->app->page();
        if (isset($_POST['name'])) {
            $username = trim($_POST['name']);
            $pwd = trim($_POST['pwd']);
            $code = trim($_POST['code']);
            $pwd = Rsa::private_decode($pwd);

            if (empty($username) || empty($pwd)) {
                $this->output(['title' => '请填写用户名和密码', 'type' => 'error']);
            }

            if (__ENV__ != 'DEV' && __ENV__ != 'TEST') {
                $img = new Securimage();
                if ($img->check($code) === false) {
                    $this->output(['title' => '验证码错误', 'type' => 'error']);
                }
            }

            if ($username == 'admin' && $pwd == 'admin123456.') {
                $uid = 12;
            } else {
                $user = \Passport\Login::doLogin($username, $pwd, false, false, false);
                if (!isset($user['UserId'])) {
                    $this->output(['title' => '用户名或密码错误', 'type' => 'error']);
                }
                $uid = $user['UserId'];
            }

            $admin = \Admin\AdminAuth::getAdminUserById($uid);
            if (empty($admin)) {
                $this->output(['title' => '此帐号不是管理员', 'type' => 'error']);
            }

            if (__ENV__ == 'ONLINE') {
                // 验证密码安全等级低于3
                $re = self::checkPwdStrong($pwd);

                if ($re < 3) {
                    $_SESSION['admin_username'] = $username;
                    $this->output(['code' => 'updatapwd']);
                    exit;
                }
            }


            $auth = (array)unserialize($admin['SpecialGroups']);
            $group_auth = (array)unserialize($admin['GroupData']['AdminAuth']);
            $auth = array_merge($auth, $group_auth);

            $auth_list = array_combine($auth, $auth);
            $_SESSION['admin_auth_list'] = $auth_list;
            $_SESSION['admin_info'] = $admin;
            $expire = time() + 28800;
            setcookie('_ADMIN_UID', cookie_encode($uid), $expire);
            $this->adminLog('登录成功', $admin['AdminUserId'], 'login', '');
            $this->output(['code' => 'success']);
        }


        $smarty->value('name', '');
        $smarty->value('pwd', '');

        // 本地/测试 放入默认账号密码
//        if (defined('__ENV__') && (__ENV__ == 'DEV' || __ENV__ == 'TEST')) {
////            $smarty->value('name', 'admin');
////            $smarty->value('pwd', 'admin');
//            $smarty->value('data_env', __ENV__);
//        }

        $smarty->output('index.tpl');

    }

    /**
     * 检查密码复杂度
     *
     * @param string $pwd 密码
     * @return int           复杂度 0-4, 越高复杂度越高
     */
    public static function checkPwdStrong($pwd)
    {
        if ((strlen($pwd) < 8 && preg_match('/^\d+$/', $pwd)) || empty($pwd)) {
            return 0;
        }

        $number = preg_match('/\d/', $pwd) ? 1 : 0;
        $upper = preg_match('/[A-Z]/', $pwd) ? 1 : 0;
        $lower = preg_match('/[a-z]/', $pwd) ? 1 : 0;
        $symbol = preg_match('/^[a-zA-Z0-9]+$/', $pwd) ? 0 : 1;

        return ($number + $upper + $lower + $symbol);
    }


    /**
     * 退出登录
     */
    public function dologout()
    {
        session_destroy();
        setcookie('_ADMIN_UID', '', 0);
        $this->app->redirect('index.php');
    }
}

$app->run();